home *** CD-ROM | disk | FTP | other *** search
- // Chap14_2.cpp
- #include <iostream.h>
- #include <string.h>
-
- class Student
- {
- public:
- Student(char *pName = "no name")
- {
- cout << "Constructing new student " << pName << "\n";
- strncpy(name, pName, sizeof(name));
- name[sizeof(name) - 1] = '\0';
- }
- Student(Student &s)
- {
- cout << "Constructing Copy of " << s.name << "\n";
- strcpy(name, "Copy of ");
- strcat(name, s.name);
- }
- ~Student()
- {
- cout << "Destructing " << name << "\n";
- }
- protected:
- char name[40];
- };
-
-
- class Tutor
- {
- public:
- Tutor(Student &s) : student(s) //invoke copy constructor...
- { //...on member student
- cout << "Constructing tutor\n";
- }
- protected:
- Student student;
- };
-
- void fn(Tutor tutor)
- {
- cout << "In function fn()\n";
- }
- int main()
- {
- Student randy("Randy");
- Tutor tutor(randy);
- cout << "Calling fn()\n";
- fn(tutor);
- cout << "Returned from fn()\n";
- return 0;
- }
-